package com.cumt.threeway.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cumt.threeway.R;
import com.cumt.threeway.constant.ThreeWayConstant;
import com.cumt.threeway.constant.ThreeWayConstant.FRAGMENT_TYPE;
import com.cumt.threeway.fragment.ClassificationFragment;
import com.cumt.threeway.fragment.HomePageFragment;
import com.cumt.threeway.fragment.MyFragment;
import com.cumt.threeway.jpush.IMessageListener;
import com.cumt.threeway.jpush.MyReceiver;
import com.cumt.threeway.model.Single;
import com.cumt.threeway.model.User;
import com.cumt.threeway.util.ThreeWayData;
import com.cumt.threeway.util.ThreeWayUtil;
import com.cumt.threeway.view.dialog.ExitAppDialog;
import com.cumt.threeway.view.widget.SelectPicPopupWindow;

import java.util.Set;
import java.util.TreeSet;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class MainManageActivity extends FragmentActivity implements View.OnClickListener,HomePageFragment.CallSetTabSelection,IMessageListener {
    private final int REQUEST_CODE_FOR_LOGIN = 3;
    private HomePageFragment mHomePageFragment;
    private ClassificationFragment mClassificationFragment;
    private MyFragment mMyFragment;
    private FragmentManager mFragmentManager;
    private FRAGMENT_TYPE mFragmentType= FRAGMENT_TYPE.UN_KNOE;
    private ImageView mIvHomePage;
    private ImageView mIvClassification;
    private ImageView mIvMy;
    private TextView mTvHomePage;
    private TextView mTvClassification;
    private TextView mTvMy;
    private View mLayoutHomePage;
    private View mLayoutClassification;
    private View mLayoutMy;
    private long exitTime=0;
    private Set<String> mTags = new TreeSet<String>();

    // 自定义的弹出框类
    SelectPicPopupWindow menuWindow;

    public ClassificationFragment getFenleifragment() {
        return mClassificationFragment;
    }

    private Handler mHandler = new Handler();
    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {

        @Override
        public void gotResult(int code, String alias, Set<String> tags) {

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_manage);
        initViews();
        initJpushTags();
        mFragmentManager = getSupportFragmentManager();
        if(savedInstanceState != null) {
            mHomePageFragment = (HomePageFragment) mFragmentManager.findFragmentByTag("HomePageFragment");
            mClassificationFragment = (ClassificationFragment) mFragmentManager.findFragmentByTag("ClassificationFragment");
            mMyFragment = (MyFragment) mFragmentManager.findFragmentByTag("MyFragment");
        }
//        mHomePageFragment = new HomePageFragment();
//        mClassificationFragment = new ClassificationFragment();
//        mMyFragment = new MyFragment();
        mFragmentType = FRAGMENT_TYPE.HOMEPAGE;
        setTabSelection();
        mHomePageFragment.setSelection(this);

        if(ThreeWayConstant.REQUEST_CODE_FOR_LOGIN == getIntent().getIntExtra("key", 0)) {
            mFragmentType = FRAGMENT_TYPE.MY;
            setTabSelection();
        }
        if(TextUtils.isEmpty(ThreeWayUtil.getInstance().getIsLoadData(this))) {
            ThreeWayData.getInstance().initData();
            ThreeWayUtil.getInstance().setLoadData(this, "data");
        }
    }

    private void initJpushTags() {
        MyReceiver.registerMsgListener(MainManageActivity.class.getName(), this);
        mTags.add("123456");
        mTags.add("963");
        JPushInterface.setAliasAndTags(getApplicationContext(), null, mTags, mAliasCallback);
    }

    private void initViews() {
        mLayoutHomePage = findViewById(R.id.shouye_layout);
        mIvHomePage = (ImageView) findViewById(R.id.shouye_image);
        mTvHomePage = (TextView) findViewById(R.id.shouye_text);
        mLayoutClassification = findViewById(R.id.fenlei_layout);
        mIvClassification = (ImageView) findViewById(R.id.fenlei_image);
        mTvClassification = (TextView) findViewById(R.id.fenlei_text);
        mLayoutMy = findViewById(R.id.wode_layout);
        mIvMy = (ImageView) findViewById(R.id.wode_image);
        mTvMy = (TextView) findViewById(R.id.wode_text);
        mLayoutHomePage.setOnClickListener(this);
        mLayoutClassification.setOnClickListener(this);
        mLayoutMy.setOnClickListener(this);
    }


    public void setTabSelection() {
        clearSelection();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
//        switch (mFragmentType) {
//            case HOMEPAGE:
//                mHomePageFragment.setSelection(this);
//                mIvHomePage.setImageResource(R.drawable.shouye2);
//                mTvHomePage.setTextColor(Color.GREEN);
//                transaction.replace(R.id.content, mHomePageFragment);
//                break;
//            case CLASSIFICATION:
//                mIvClassification.setImageResource(R.drawable.fenlei2);
//                mTvClassification.setTextColor(Color.GREEN);
//                transaction.replace(R.id.content, mClassificationFragment);
//                break;
//            case MY:
//                mIvMy.setImageResource(R.drawable.wode2);
//                mTvMy.setTextColor(Color.GREEN);
//                transaction.replace(R.id.content, mMyFragment);
//                break;
//        }
//        transaction.commit();
        hideFragments(transaction);
        switch (mFragmentType) {
            case HOMEPAGE:
                mIvHomePage.setImageResource(R.drawable.shouye2);
                mTvHomePage.setTextColor(Color.GREEN);
                if(mHomePageFragment == null) {
                    mHomePageFragment = HomePageFragment.newInstance();
                    transaction.add(R.id.content, mHomePageFragment,"HomePageFragment");
                } else {
                    transaction.show(mHomePageFragment);
                }
                break;
            case CLASSIFICATION:
                mIvClassification.setImageResource(R.drawable.fenlei2);
                mTvClassification.setTextColor(Color.GREEN);
                if(mClassificationFragment == null) {
                    mClassificationFragment = ClassificationFragment.newInstance();
                    transaction.add(R.id.content, mClassificationFragment, "ClassificationFragment");
                } else {
                    transaction.show(mClassificationFragment);
                }
                break;
            case MY:
                mIvMy.setImageResource(R.drawable.wode2);
                mTvMy.setTextColor(Color.GREEN);
                if(mMyFragment == null) {
                    mMyFragment = MyFragment.newInstance();
                    transaction.add(R.id.content, mMyFragment, "MyFragment");
                } else {
                    transaction.show(mMyFragment);
                }
                break;
        }
        transaction.commitAllowingStateLoss();

    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mHomePageFragment != null) {
            transaction.hide(mHomePageFragment);
        }
        if (mClassificationFragment != null) {
            transaction.hide(mClassificationFragment);
        }
        if (mMyFragment != null) {
            transaction.hide(mMyFragment);
        }
    }

    private void clearSelection() {
        mIvHomePage.setImageResource(R.drawable.shouye1);
        mTvHomePage.setTextColor(Color.parseColor("#000000"));
        mLayoutHomePage.setBackgroundColor(Color.WHITE);
        mIvClassification.setImageResource(R.drawable.fenlei1);
        mTvClassification.setTextColor(Color.parseColor("#000000"));
        mLayoutClassification.setBackgroundColor(Color.WHITE);
        mIvMy.setImageResource(R.drawable.wode);
        mTvMy.setTextColor(Color.parseColor("#000000"));
        mLayoutMy.setBackgroundColor(Color.WHITE);
    }

//点击两次退出app

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
//			exit();
            ExitAppDialog exitAppDialog = ExitAppDialog.newInstance();
            exitAppDialog.show(getSupportFragmentManager(), "exitAppDialog");
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if((System.currentTimeMillis()-exitTime)>2000){
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime=System.currentTimeMillis();
        }else{
            finish();
            System.exit(0);
        }

    }

    public void goOtherActivity(int position) {
        Single single = new Single();
        switch (position) {
            case 0:
                Intent intent = new Intent(this, DetailActivity.class);
                intent.putExtra("single", ThreeWayData.getInstance().getActivitiesInfo(1).get(0));
                startActivity(intent);
                break;
            case 1:
                Intent intent1 = new Intent(this, DetailActivity.class);
                intent1.putExtra("single", ThreeWayData.getInstance().getActivitiesInfo(3).get(0));
                startActivity(intent1);
                break;
            case 2:
                Intent intent2 = new Intent(this, DetailActivity.class);
                intent2.putExtra("single", ThreeWayData.getInstance().getActivitiesInfo(3).get(1));
                startActivity(intent2);
                break;
            case 3:
                Intent intent3 = new Intent(this, DetailActivity.class);
                intent3.putExtra("single", ThreeWayData.getInstance().getActivitiesInfo(1).get(1));
                startActivity(intent3);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shouye_layout:
                if(mFragmentType != FRAGMENT_TYPE.HOMEPAGE) {
                    mFragmentType = FRAGMENT_TYPE.HOMEPAGE;
                    setTabSelection();
                }
                break;
            case R.id.fenlei_layout:
                if(mFragmentType != FRAGMENT_TYPE.CLASSIFICATION) {
                    mFragmentType = FRAGMENT_TYPE.CLASSIFICATION;
                    setTabSelection();
                }
                break;
            case R.id.wode_layout:
                if(mFragmentType != FRAGMENT_TYPE.MY) {
                    mFragmentType = FRAGMENT_TYPE.MY;
                    setTabSelection();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void callSetTab(final int position) {
        mFragmentType = FRAGMENT_TYPE.CLASSIFICATION;
        setTabSelection();
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mClassificationFragment.setPageItem(position);
            }
        });
    }

    @Override
    public void onReceiveInfo(String msg) {

    }

    @Override
    public void onReceiveInfo(String messageTitle, String messageContent, String messageUrl) {

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CODE_FOR_LOGIN) {
            if(resultCode == RESULT_OK){
                User user = (User) data.getSerializableExtra("user");
                mMyFragment.setThirdLoginUserInfo(user);
            } else if(resultCode == -2) {
                User user = (User) data.getSerializableExtra("user");
                mMyFragment.setUserInfo(user);
            } else if(resultCode == -3) {//个人中心换完头像返回
                mMyFragment.setUserIco();
            }
        }
    }
}
